

$('#search').bind('keypress',function(event){
    if(event.keyCode === 13) {
        ajaxLoadDateTabel();
    }
});

/**
 * 分页结果参数封装
 * @param index 当前页码
 * @param page 服务端结果展示对象
 * @param keys key值数组
 * @param operation 操作
 * @param replaceKey 需要替换处理的key值
 * @param replaceMethod 处理函数
 * @param replaceKey1 需要替换处理的key值
 * @param replaceMethod1 处理函数
 * @returns {string}
 */
function pageCount(index,page,keys,operation,replaceKey,replaceMethod,replaceKey1,replaceMethod1) {
    var rows = page.rowList;
    var replaceKey = replaceKey || null;
    var replaceKey1 = replaceKey1 || null;
    var tableHtml = "";
    for(var j = 0; j < rows.length ; j++){
        var className = j%2 ===0 ?"odd":"even";
        var obj = JSON.parse(JSON.stringify(rows[j]));
        tableHtml += '<tr role="row" class="' + className + '">';
        for (var i = 0; i < keys.length ; i++) {
            for (var key in obj) {
                if (key === keys[i]) {
                    if (replaceKey != null && key.toUpperCase().indexOf((replaceKey).toUpperCase()) >= 0) {
                        tableHtml += replaceMethod(obj[key]);
                    } else if (replaceKey1 != null && key.toUpperCase().indexOf((replaceKey1).toUpperCase()) >= 0) {
                        tableHtml += replaceMethod1(obj[key]);
                    } else if (key.toUpperCase().indexOf(("state").toUpperCase()) >= 0) {
                        tableHtml += stateHtml(obj[key]);
                    } else if (key.toUpperCase().indexOf(("image").toUpperCase()) >= 0 || key.toUpperCase().indexOf(("images").toUpperCase()) >= 0 || key.toUpperCase().indexOf(("img").toUpperCase()) >= 0 || key.toUpperCase().indexOf(("icon").toUpperCase()) >= 0) {
                        tableHtml += '<td><img src="' + obj[key] + '" /></td>';
                    } else if (key.toUpperCase().indexOf(("content").toUpperCase()) >= 0) {
                        tableHtml += '<td class="content">' + obj[key] + '</td>';
                    } else {
                        var value = obj[key] || "";
                        tableHtml += '<td>' + value + '</td>';
                    }
                }
            }
        }
        if(operation!=null&&operation!==""){
            tableHtml += operation.replace(/OPERATIONID/g,obj[keys[0]]);
        }
        tableHtml += '</tr>';
    }
    $("#dataTableRows").html(tableHtml);
    $("#pageNumber").html( pageNumber(index,page));
    $("#rowCount").html(rowCount(page.rowCount));
}

/**
 * 页码方法分装
 * @param index
 * @param page
 * @returns {string}
 */
function pageNumber(index,page) {
    var numbers = page.numbers;
    var previous = index<=1?1:index-1;
    var previousState = index<=1?"disabled":"";
    var pagetml = '<li class="paginate_button page-item previous '+previousState+'" id="m_table_1_previous">' +
        '<a href="#" onclick="ajaxLoadDateTabel('+previous+')" aria-controls="m_table_1" data-dt-idx="0" tabindex="0" class="page-link"><i class="la la-angle-left"></i></a></li>';
    for (var i = 0; i < numbers.length; i++) {
        var className = numbers[i] === index ? "active disabled" : "";
        pagetml +='<li class="paginate_button page-item '+className+'"><a href="#"  onclick="ajaxLoadDateTabel('+numbers[i]+')" aria-controls="m_table_1"\n' +
            'data-dt-idx="'+(i+1)+'" tabindex="0"\n' +
            'class="page-link"><font\n' +
            'style="vertical-align: inherit;"><font style="vertical-align: inherit;">'+(i+1)+'</font></font></a>\n' +
            '</li>'
    }
    var next = index>=page.pageCount?index:index+1;
    var nextState = index>=page.pageCount?"disabled":"";
    pagetml +='<li class="paginate_button page-item next '+nextState+'" id="m_table_1_next"><a href="#"\n' +
        ' onclick="ajaxLoadDateTabel('+next+')"  aria-controls="m_table_1" data-dt-idx="8" tabindex="0" class="page-link"><i class="la la-angle-right"></i></a></li>';
    return pagetml;
}

/**
 * 统计信息封装
 * @param rowCount
 * @returns {string}
 */
function rowCount(rowCount) {
    return ' <font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 符合条件的数据'+rowCount+'个条目</font></font>'
}

/**
 * 清空结果
 */
function empty() {
    $("#dataTableRows").html("");
    $("#pageNumber").html("");
    $("#rowCount").html("");
}

function stateHtml(state) {
    if(state===1){
        return '<td><span class="m-badge  m-badge--success m-badge--wide"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用中</font></font></span></td>';
    }
    return '<td><span class="m-badge m-badge--danger m-badge--wide"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">已失效</font></font></span></td>';
}
